﻿#include "dialog_LECalibrator.h"
#include "ui_dialog_LECalibrator.h"


Dialog_LECalibrator::Dialog_LECalibrator(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog_LECalibrator)
{
    ui->setupUi(this);
    setWindowTitle(u8"光栅尺线性矫正");
    ui->plainTextEdit_x->clear();
    ui->plainTextEdit_y->clear();

    connect(ui->pushButton_addPoint_x, SIGNAL(clicked(bool)), this, SLOT(addPoint_x()));
    connect(ui->pushButton_addPoint_y, SIGNAL(clicked(bool)), this, SLOT(addPoint_y()));
    connect(ui->pushButtonWriteSettings_x, SIGNAL(clicked(bool)), this, SLOT(writeSettings_x()));
    connect(ui->pushButtonWriteSettings_y, SIGNAL(clicked(bool)), this, SLOT(writeSettings_y()));
}

Dialog_LECalibrator::~Dialog_LECalibrator()
{
    delete ui;
}

void Dialog_LECalibrator::updateEncoder(double x_mm, double y_mm)
{
    ui->label_encoder_x->setNum(x_mm);
    ui->label_encoder_y->setNum(y_mm);
}

void Dialog_LECalibrator::addPoint_x()
{
    ui->plainTextEdit_x->appendPlainText(ui->label_encoder_x->text());
}

void Dialog_LECalibrator::addPoint_y()
{
    ui->plainTextEdit_y->appendPlainText(ui->label_encoder_y->text());
}

void Dialog_LECalibrator::writeSettings_x()
{
    ledata_x.clear();
    QString str = ui->plainTextEdit_x->toPlainText();
    QStringList list = str.split("\n", QString::SkipEmptyParts);
    QStringListIterator javaStyleIterator(list);
    while (javaStyleIterator.hasNext())
    {
        bool ok = 0;
        double x = javaStyleIterator.next().toDouble(&ok);
        if(ok)
        {
            ledata_x.append(x);
        }
    }

    QString path = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
    QSettings settings( path + "/JHPhoto.ini", QSettings::IniFormat, this );
    ledata_x.writeToSettings(settings, "Encoder/AxisX_");
}

void Dialog_LECalibrator::writeSettings_y()
{
    ledata_y.clear();
    QString str = ui->plainTextEdit_y->toPlainText();
    QStringList list = str.split("\n", QString::SkipEmptyParts);
    QStringListIterator javaStyleIterator(list);
    while (javaStyleIterator.hasNext())
    {
        bool ok = 0;
        double x = javaStyleIterator.next().toDouble(&ok);
        if(ok)
        {
            ledata_y.append(x);
        }
    }

    QString path = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
    QSettings settings( path + "/JHPhoto.ini", QSettings::IniFormat, this );
    ledata_y.writeToSettings(settings, "Encoder/AxisY_");
}

